package com.gitee.attendance.service.impl;


import com.gitee.attendance.entity.User;
import com.gitee.attendance.service.UserServiceI;
import com.gitee.attendance.util.rides.RedisUtil;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("UserService")
@Transactional
public class UserServiceImpl implements UserServiceI {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private RedisUtil redis;


    public String addUser(User user) throws Exception {
        if (redis.set(User.REDIS_KEY + user.getUserCode(), new JSONObject(user).toString())) {
            String result = redis.get(User.REDIS_KEY + user.getUserCode()).toString();
            try {
                new JSONObject(result);
            } catch (Exception e) {
                return "ERROR:录入结果格式异常 ";
            }
            logger.debug(result.length() > 200 ? result.substring(0, 200) + "..." : result);
            return "SUCCESS:添加 " + user.getUserName() + " 成功";
        } else {
            return "SUCCESS:添加 " + user.getUserName() + " 失败";
        }

    }

}